package com.tongguan.paypos.fragment;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.tongguan.paypos.R;
import com.tongguan.paypos.adapter.ManageAdapter;
import com.tongguan.paypos.base.BaseFragment;
import com.tongguan.paypos.entity.Manage;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Joylive on 2016/12/14 .
 */

public class ManageGVFragment extends BaseFragment {

    public static final String MANAGE_FRAGMENT = "manage_fragment";
    public static final String MANAGE_TITLE = "manage_title";

    @BindView(R.id.gv_manage)
    GridView mGvManage;
    private ArrayList<Manage> manages;
    private String title;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_manage, container, false);
        ButterKnife.bind(this, rootView);
        Bundle arguments = getArguments();
        if (arguments != null) {
            manages =arguments.getParcelableArrayList(MANAGE_FRAGMENT);
            title = arguments.getString(MANAGE_TITLE);
            ManageAdapter adapter = new ManageAdapter(frmCtx, manages);
            mGvManage.setAdapter(adapter);
            initEvent();
        }
        if (!TextUtils.isEmpty(title)){
            setTitle(title);
        }
        return rootView;
    }


    private void initEvent() {
        mGvManage.setOnItemClickListener((parent, view, position, id) -> {
            Fragment instantiate = Fragment.instantiate(frmCtx, manages.get(position).getFragmentName());
            getFragmentManager().beginTransaction().replace(R.id.container,instantiate).addToBackStack(null).commit();
        });
    }
}
